package com.dsr.service.impl;

import com.dsr.consts.PageResult;
import com.dsr.consts.Result;
import com.dsr.dao.RoleAndModelDao;
import com.dsr.entity.RoleAndModel;
import com.dsr.service.RoleAndModelService;
import com.dsr.util.IdWorker;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.persistence.Id;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * (RoleAndModel)表服务实现类
 *
 * @author dsr
 * @since 2020-09-26
 */
@Service("roleAndModelService")
public class RoleAndModelServiceImpl implements RoleAndModelService {
    @Autowired
    private RoleAndModelDao roleAndModelDao;

    @Autowired
    private IdWorker idWorker;

    @Override
    public Result getEntity(String id) {
        RoleAndModel entity = roleAndModelDao.selectByPrimaryKey(id);
        return Result.success(entity);
    }

    @Override
    public Result save(RoleAndModel roleAndModel) {
        String roleId = roleAndModel.getRoleId();
        roleAndModelDao.deleteByRoleId(roleId);
        List<RoleAndModel> roleAndModelList=new ArrayList<>();

        Arrays.asList( roleAndModel.getModelId().split(",")).forEach(item->{
            if(StringUtils.isNotEmpty(item)){
                RoleAndModel entity = new RoleAndModel();
                entity.setRoleId(roleId);
                entity.setModelId(item);
                roleAndModelList.add(entity);
            }
        });
        if(roleAndModelList.size()>0){
            roleAndModelDao.insertModelAndRoles(roleAndModelList);
        }
        return Result.success("保存成功");
    }

    @Override
    public Result update(RoleAndModel roleAndModel) {
        roleAndModelDao.updateByPrimaryKeySelective(roleAndModel);
        return Result.success("修改成功");

    }

    @Override
    public Result delete(String id) {
        if (StringUtils.isNotEmpty(id)) {
            List<String> ids = Arrays.asList(id.split(","));
            Example example = new Example(RoleAndModel.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andIn("id", ids);
            roleAndModelDao.deleteByExample(example);
            return Result.success("删除成功");
        }
        return Result.error("请选择要删除的行");
    }

    @Override
    public Result query(int page, int size, RoleAndModel roleAndModel) {
        PageHelper.startPage(page, size);
        List<RoleAndModel> entityList = roleAndModelDao.select(roleAndModel);
        PageInfo<RoleAndModel> pageInfo = new PageInfo<>(entityList);
        return Result.success(new PageResult<RoleAndModel>(pageInfo.getTotal(), pageInfo.getList()));
    }

    @Override
    public Result list() {
        return Result.success(roleAndModelDao.selectAll());
    }

    @Override
    public Result getAuth(String roleId) {
        RoleAndModel roleAndModel = new RoleAndModel();
        return Result.success(roleAndModelDao.getAuth(roleId));
    }
}